package com.zfei.gfb.controller;

import com.zfei.gfb.bean.Const;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.core.Auth;
import com.zfei.gfb.core.HttpJson;
import com.zfei.gfb.core.LogResult;
import com.zfei.gfb.util.OssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

//@Auth(type = {Const.VILLAGE_MANAGER, Const.TOWN_MANAGER, Const.COMPANY, Const.VILLAGER})
@Api(tags = "图片上传")
@RestController
@RequestMapping("/web/sys")
public class SysController {

    @ApiOperation(value = "图片上传")
    @LogResult
    @HttpJson("/upload")
    public Object add(@RequestParam(value = "file", required = false) MultipartFile file) {

        if (file == null || file.isEmpty()) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }

        byte[] byteArr;
        try {
            byteArr = file.getBytes();
        } catch (IOException e) {
            throw new StatusException(Status.FILE_NOT_FOUND, e);
        }
        InputStream inputStream = new ByteArrayInputStream(byteArr);
        return OssUtil.upload(inputStream, "common", file.getOriginalFilename());


    }
}
